package stiletto.user.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Created with IntelliJ IDEA.
 * User: 82022170
 * Date: 13. 10. 28
 * Time: 오후 3:42
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class User {
    @Id
    @Column(length = 150)
    private String email;
    @Column(length = 50, nullable = false)
    private String screenName;
    @Column(length = 100, nullable = false)
    private String password;
    @Column(nullable = false)
    private Long lastLogin;
    @Column(nullable = false)
    private Boolean isActive;

    /**
     * JPA가 요구해서 만든 기본 생성자. 이 생성자로 객체를 초기화한 이후엔 반드시
     * setter 메소드로 속성 값을 설정하시오. 그렇지 않으면 에러 발생함.
     */
    public User() {
        this("", "", "", false);
    }

    /**
     * @param email
     * @param screenName
     * @param password
     * @param isActive
     */
    public User(String email, String screenName, String password, boolean isActive) {
        setEmail(email);
        setScreenName(screenName);
        setPassword(password);
        setActive(isActive);
        lastLogin = System.currentTimeMillis();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        // todo : email 형식 검사 추가
        this.email = email;
    }

    public String getScreenName() {
        return screenName;
    }

    public void setScreenName(String screenName) {
        this.screenName = screenName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        // todo : 암호화 추가
        this.password = password;
    }

    public boolean isActive() {
        return isActive;
    }

    /**
     * 이 사용자를 활성 / 비활성화시킨다.
     *
     * @param isActive 사용자 정보 활성화 여부
     */
    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public void activate() {
        setActive(true);
    }

    public void deactivate() {
        setActive(false);
    }

    /**
     * @return 사용자가 시스템에 접근한 최근 시각(밀리초)
     */
    public long getLastLogin() {
        return lastLogin;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (!email.equals(user.email)) return false;
        if (!isActive.equals(user.isActive)) return false;
        if (!password.equals(user.password)) return false;
        if (!screenName.equals(user.screenName)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = email.hashCode();
        result = 31 * result + screenName.hashCode();
        result = 31 * result + password.hashCode();
        result = 31 * result + isActive.hashCode();

        return result;
    }

    @Override
    public String toString() {
        return "User{" +
                "email='" + email + '\'' +
                ", screenName='" + screenName + '\'' +
                ", password='" + password + '\'' +
                ", lastLogin=" + lastLogin +
                ", isActive=" + isActive +
                '}';
    }
}
